<template>
  <div>
    <div class="loading" v-if="!(loading1 && loading2)">数据加载中…</div>
    <div id="listing" v-if="loading1 && loading2">
      <div class="header">
        <div class="coverimg">
          <img :src="songListDetail.coverImgUrl" alt="" />
        </div>
        <div class="info">
          <div class="title">{{ songListDetail.name }}</div>
          <div class="ar">
            <span>
              <img :src="songListDetail.avatarUrl" alt="" />
            </span>
            <span @click="touserpage">{{ songListDetail.nickname }}</span>
            <span>{{ songListDetail.time }}创建</span>
          </div>
          <div class="play" @click="playlist">播放全部</div>
          <div class="detail">
            <div>
              标签：
              <span v-for="(b, index) of songListDetail.tags" :key="index">{{
                b
              }}</span>
            </div>
            <div>
              歌曲：{{ songListDetail.trackCount }} 播放：{{
                songListDetail.playCount
              }}
            </div>
            <div>简介：{{ songListDetail.description }}</div>
          </div>
        </div>
      </div>
      <div class="content">
        <ul>
          <li id="biaoti">
            <span id="index"></span>
            <span id="name">音乐标题</span>
            <span id="author">歌手</span>
            <span id="album">专辑</span>
            <span id="td">时长</span>
          </li>
          <li
            v-for="(i, index) in songData"
            :key="index"
            @dblclick="clicked(i.id)"
          >
            <span id="index">{{ index + 1 }}</span>
            <span id="name">{{ i.name }}</span>
            <span id="author">{{ i.author }}</span>
            <span id="album">{{ i.album }}</span>
            <span id="td">{{ i.time }}</span>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { mapState } from "vuex";
import formatSeconds from "../js/time";
import getDay from "../js/date";
import LoginVue from "../components/Login.vue";
export default {
  name: "Listing",
  data() {
    return {
      songData: [],
      songListDetail: {},
      loading1: false,
      loading2: false,
    };
  },
  computed: {
    ...mapState("songAbout", ["index", "singinglist"]),
    songId() {
      return this.$route.query.songListId;
    },
    fresh() {
      return this.$route.query.isRefresh;
    },
  },
  methods: {
    updata() {
      this.songData = [];
      this.songListDetail = {};
      axios
        .get("http://localhost:8013/api/playlist/track/all?id=" + this.songId)
        .then((res) => {
          res.data.songs.forEach((item) => {
            const obj = {};
            obj.id = item.id;
            obj.name = item.name;
            obj.author = item.ar[0].name;
            obj.album = item.al.name;
            let t = formatSeconds(item.dt);
            obj.time = t;
            this.songData.push(obj);

            this.loading1 = true;
          });
        });
      axios
        .get("http://localhost:8013/api/playlist/detail?id=" + this.songId)
        .then((res) => {
          this.songListDetail.userId = res.data.playlist.userId;
          this.songListDetail.nickname = res.data.playlist.creator.nickname;
          this.songListDetail.name = res.data.playlist.name;
          this.songListDetail.avatarUrl = res.data.playlist.creator.avatarUrl;
          this.songListDetail.coverImgUrl = res.data.playlist.coverImgUrl;
          this.songListDetail.time = getDay(res.data.playlist.createTime);
          this.songListDetail.tags = res.data.playlist.tags;
          this.songListDetail.trackCount = res.data.playlist.trackCount;
          this.songListDetail.playCount = res.data.playlist.playCount;
          res.data.playlist.description == null
            ? (this.songListDetail.description = "无")
            : (this.songListDetail.description = res.data.playlist.description);
          // this.songListDetail.description = res.data.playlist.description;

          this.loading2 = true;
        });
    },

    playlist() {
      this.$confirm('当前操作将替换全部的音乐列表，确定继续吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'info'
        }).then(() => {
          axios
          .get("http://localhost:8013/api/playlist/track/all?id=" + this.songId)
          .then((res) => {
            //将歌单添加到音乐列表
            this.$store.commit("songAbout/UPDATESONG", res.data.songs);
            //加载音乐
            this.$store.dispatch("songAbout/playsongid", 0);
            //改变状态
            this.$store.commit("songAbout/CHANGE", true);
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
    },
    clicked(id) {
      //改变播放状态 playing改为true
      this.$store.commit("songAbout/CHANGE", true);
      //遍历当前播放列表数组，查看点击的歌曲是否存在当前歌单
      let result = this.singinglist.findIndex((item) => {
        return item.id === id;
      });
      if (result == -1) {
        //当前播放列表不包含该歌曲,则添加到当前列表
        axios
          .get("http://localhost:8013/api/song/detail?ids=" + id)
          .then((res) => {
            this.singinglist.splice(this.index + 1, 0, res.data.songs[0]);
            this.$store.dispatch("songAbout/playsongid", this.index + 1);
          });
      } else {
        //当前播放列表包含该歌曲
        this.$store.dispatch("songAbout/playsongid", result);
      }
    },
    touserpage() {
      this.$bus.$emit("routeToUserProfile", this.songListDetail.userId);
    },
  },

  activated() {
    this.loading1 = false;
    this.loading2 = false;
    this.updata();
  },

  beforeRouteUpdate(to, from, next) {
    if (to.query.isRefresh) {
      this.$nextTick(() => {
        this.loading1 = false;
        this.loading2 = false;
        this.updata();
        // this.isShowPage = true;
      });
    }
    next();
  },
};
</script>

<style scoped>
#listing {
  padding-bottom: 100px;
}
.content ul li {
  text-align: left;
  height: 40px;
  line-height: 40px;
  font-size: 16px;
}
.content ul li:nth-of-type(even) {
  background: #fcfcfc;
}
.content ul li:hover {
  background: #fafafa;
}
.content #biaoti {
  font-weight: 600;
}
.content ul li span {
  display: inline-block;
  height: 40px;
  font-size: 14px;
  color: #666;
  overflow: hidden;
}
.content ul li #index {
  width: 5%;
  color: #aaa;
  text-align: center;
}
.content ul li #name {
  width: 42%;
}
.content ul li #author {
  width: 18%;
}
.content ul li #album {
  width: 25%;
}
.content ul li #td {
  width: 10%;
}

.header {
  height: 245px;
  /* background: powderblue; */
}
.header .coverimg {
  float: left;
  height: 200px;
  width: 200px;
  margin: 10px 20px 0 10px;
  border-radius: 8px;
  overflow: hidden;
}
.header .coverimg img {
  width: 100%;
}
.header .info {
  height: 200px;
  margin-left: 220px;
  padding-top: 10px;
  text-align: left;
}
.header .info .title {
  height: 45px;
  line-height: 45px;
  font-weight: 600;
  font-size: 22px;
  color: #000;
}
.header .info .ar {
  height: 25px;
  line-height: 20px;
  cursor: pointer;
}
.header .info .ar img {
  float: left;
  width: 25px;
  height: 25px;
  border-radius: 50%;
}
.header .info .ar span {
  font-size: 12px;
  margin-left: 10px;
}
.header .info .ar span:nth-child(2) {
  color: rgb(34, 116, 209);
  cursor: pointer;
}
.play {
  cursor: pointer;
  background: steelblue;
  text-align: center;
  border-radius: 20px;
  width: 120px;
  height: 30px;
  line-height: 30px;
  color: #fff;
  margin: 10px 0px 0px 10px;
}
.detail {
  height: 85px;
  margin-top: 5px;
}
.detail div {
  height: 30px;
  line-height: 30px;
  font-size: 13px;
}
.detail div:nth-child(1) span {
  font-size: 13px;
  padding-right: 8px;
}
.detail div:nth-child(3) {
  background: #fff;
  margin-right: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>